package com.srpingde.hhh;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.application.HHHDataManage;
import com.srpingde.hhh.bean.ShouHuoBean;
import com.srpingde.hhh.bean.ShouHuoBeanData;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class ShouHuoListActivity extends Activity {
	ImageView back;
	ListView shouhuo_lv;
	HHHIml weiQuIml;
	ShouHuoBean shouHuoBean;
	ArrayList<ShouHuoBeanData> shouHuoBeanDatas;
	ShouHuoAdapter adapter;
	String name,address,tel,content;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_shou_huo_list);
		back = (ImageView) findViewById(R.id.back);
		shouhuo_lv = (ListView) findViewById(R.id.shouhuo_lv);
		weiQuIml = new HHHIml();
		if(getIntent().getExtras()!=null){
			content = getIntent().getStringExtra("content");
		}
		Button goumai = (Button) findViewById(R.id.goumai);
		if(content == null){
			goumai.setVisibility(View.GONE);
		}
		shouHuoBeanDatas = new ArrayList<ShouHuoBeanData>();
		layout();
		shouhuo_lv.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
					final int arg2, long arg3) {
				// TODO Auto-generated method stub
				new AlertDialog.Builder(ShouHuoListActivity.this).setTitle("提示").setMessage("删除地址吗？").setPositiveButton("确定", new OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						String url = HHHInterface.httpHeader + "api.php?tab=address&act=del&id="+shouHuoBean.getRows().get(arg2).getId()+"&uid="+HhhApp.getInstance().getUid();
						weiQuIml.loginHttp(ShouHuoListActivity.this,url, new AsyncHttpResponseHandler(){
							@Override
							public void onSuccess(String content) {
								super.onSuccess(content);
								Log.e("mytag", content+"================收藏返回的数据");
								try {
									JSONObject jsonObject = new JSONObject(content);
									if(jsonObject.getBoolean("return")){
										shouHuoBeanDatas.remove(arg2);
										shouhuo_lv.setAdapter(new ShouHuoAdapter(shouHuoBeanDatas, ShouHuoListActivity.this));
										HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "删除成功");
									}else{
										HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "删除失败");
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});	
					}
				}).setNegativeButton("取消", null).create().show();
				return false;
			}
		});
		shouhuo_lv.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				name = shouHuoBeanDatas.get(arg2).getName();
				tel = shouHuoBeanDatas.get(arg2).getTel();
				address = shouHuoBeanDatas.get(arg2).getAddress();
			}
		});
	}
	public void onClick(View v){
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.add_address:
			startActivity(new Intent(ShouHuoListActivity.this, ShouHuoXinXiActivity.class));
			break;
		case R.id.goumai:
			if(content==null){
				HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "您还没有选择要购买的商品奥！");
			}else{
				if(shouHuoBeanDatas.size()!=0){
					if(name!=null&&tel!=null&&address!=null){
						String url = HHHInterface.httpHeader + "api.php?act=addtocart&uid="+HhhApp.getInstance().getUid()+"&name="+name+"&address="+address+"&tel="+tel+"&pay=3&detail=";
					     try {
							url=url+URLEncoder.encode(content, "utf-8");
						} catch (UnsupportedEncodingException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						weiQuIml.xiaDingDan(url,ShouHuoListActivity.this, new AsyncHttpResponseHandler(){

							@Override
							public void onSuccess(String content) {
								// TODO Auto-generated method stub
								super.onSuccess(content);
								try {
									JSONObject jsonObject = new JSONObject(content);
									if(jsonObject.getBoolean("return")){
										for(int q = 0;q<GouWuCheActivity.position.size();q++)
										{
											delete2(GouWuCheActivity.gouWuChe.getRows().get(GouWuCheActivity.position.get(q)).getPid(),GouWuCheActivity.gouWuChe.getRows().get(GouWuCheActivity.position.get(q)).getStyle());
											GouWuCheActivity.gouWuChe.getRows().remove(GouWuCheActivity.position.get(q));					 
										}
										//WeiQuApp.getInstance().showDialog(ShouHuoListActivity.this, "下订单成功，卖家会主动联系您进行确认！");
										Intent intent = new Intent(ShouHuoListActivity.this,DingDanActivity.class);
										startActivity(intent);
										name = null;
										tel = null;
										address = null;
									}else{
										HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "购买失败");
									}
								} catch (JSONException e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}
						});
					}else{
						HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "请选择收货地址");
					}
				}else{
					HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "请添加收货地址");
				}
			}
			break;
		}
	}
	
	public void delete2(String pid,String style) {
		String str = HHHInterface.httpHeader + "api.php?tab=cart&act=del&pid="
				+ pid + "&uid=" + HhhApp.getInstance().getUid() + "&style=" + style;
		Log.e("删除", "pid"+pid+"uid"+HhhApp.getInstance().getUid());
		weiQuIml.gouWuChedeleteHttp(str, ShouHuoListActivity.this,
				new AsyncHttpResponseHandler() {
					@Override
					public void onSuccess(String content) {
						super.onSuccess(content);
						Log.e("mytag", content + "================收藏返回的数据");
						try {
							JSONObject jsonObject = new JSONObject(content);
							if (jsonObject.getBoolean("return")) {
								//WeiQuApp.getInstance().showDialog(GouWuCheActivity.this, "删除成功");
							} else {
								HhhApp.getInstance().showDialog(ShouHuoListActivity.this, "删除失败");
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
	
	
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		getData();
	}
	
	private void getData(){
		String url = HHHInterface.httpHeader + "api.php?tab=address&act=data&uid="+HhhApp.getInstance().getUid()+"&pagesize=10&page=1";
    	weiQuIml.shouHuoList(url,ShouHuoListActivity.this,new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				Log.e("mytag",content+"=======分类的数据=");
				shouHuoBean = HHHDataManage.getInstance().getShouHuoBean();
				shouHuoBeanDatas = shouHuoBean.getRows();
				shouhuo_lv.setAdapter(new ShouHuoAdapter(shouHuoBeanDatas, ShouHuoListActivity.this));
			}
		});
    }
	class ShouHuoAdapter extends BaseAdapter{
		ArrayList<ShouHuoBeanData> shouHuoBeanDatas = new ArrayList<ShouHuoBeanData>();
		Context context;
		public ShouHuoAdapter(ArrayList<ShouHuoBeanData> shouHuoBeanDatas,
				Context context) {
			super();
			this.shouHuoBeanDatas = shouHuoBeanDatas;
			this.context = context;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return shouHuoBeanDatas.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(int arg0, View view, ViewGroup arg2) {
			// TODO Auto-generated method stub
			view = LayoutInflater.from(context).inflate(R.layout.shouhuoitem, null);
			TextView name = (TextView) view.findViewById(R.id.item_name); 
			TextView tel = (TextView) view.findViewById(R.id.item_tel); 
			TextView address = (TextView) view.findViewById(R.id.item_address); 
			name.setText("收件人： "+shouHuoBeanDatas.get(arg0).getName());
			tel.setText("联系方式： "+shouHuoBeanDatas.get(arg0).getTel());
			address.setText("地址： "+shouHuoBeanDatas.get(arg0).getAddress());
			return view;
		}
	}
	private void layout() {
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
	}
}
